﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using WebBanThuoc.DTO;

namespace WebBanThuoc.DAO
{
    public class HinhAnh_DAO
    {

        private SqlConnection connection;
        //public bool hasData { set; get; }
        public HinhAnh_DAO(SqlConnection connection)
        {
            this.connection = connection;
        }

        private List<HinhAnh> getListBy(string whereCom, string orderCom)
        {
            List<HinhAnh> data = new List<HinhAnh>();
            string comStr = "Select * from [HinhAnh]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    while (dap.DaReader.Read())
                    {
                        HinhAnh obj = new HinhAnh();

                        obj.IDHinhAnh = Convert.ToInt32(dap.DaReader["IDHinhAnh"]);
                        obj.DuongDan = dap.DaReader["DuongDan"].ToString();
                        obj.ChuThich = dap.DaReader["ChuThich"].ToString();
                        obj.MaSanPham = dap.DaReader["MaSanPham"].ToString();                        
                        
                        data.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public List<HinhAnh> getListByKey()
        {
            return getListBy("", " [IDHinhAnh] asc");
        }

        public List<HinhAnh> getListByKey(string MaSanPham)
        {
            return getListBy(" [MaSanPham]='" + MaSanPham + "' ", " [IDHinhAnh] asc");
        }

        public int createIDHinhAnh()
        {
            int result = 1;
            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, "SELECT [dbo].[fnc_CreateNew_IDHinhAnh] () as col");
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    dap.DaReader.Read();
                    result = Convert.ToInt32(dap.DaReader["col"]);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private int delete(string whereCom)
        {
            string comUpdate = "DELETE FROM [HinhAnh] ";
            if (whereCom.Trim() != "")
                comUpdate += " where " + whereCom;
            try
            {
                IDataProvider dap = new DataProvider(this.connection);
                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comUpdate);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int deleteByKey(int IDHinhAnh)
        {
            return delete(" [IDHinhAnh]=" + IDHinhAnh);
        }
        public int deleteByKey(string MaSanPham)
        {
            return delete(" [MaSanPham]='" + MaSanPham + "' ");
        }

        public int add(HinhAnh obj)
        {
            string comQuery = "INSERT INTO [HinhAnh] ([IDHinhAnh],[DuongDan],[ChuThich]"
                + ",[MaSanPham])"
                + " VALUES (@IDHinhAnh,@DuongDan,@ChuThich,@MaSanPham)";                
            try
            {
                IDataProvider dap = new DataProvider(connection);
                if (obj.MaSanPham == null || obj.MaSanPham == "")
                {
                    obj.IDHinhAnh = createIDHinhAnh();
                }
                dap.AddParameters("@IDHinhAnh", obj.IDHinhAnh, System.Data.SqlDbType.Int);
                dap.AddParameters("@DuongDan", obj.DuongDan, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@ChuThich", obj.ChuThich, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@MaSanPham", obj.MaSanPham, System.Data.SqlDbType.NVarChar);               

                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comQuery);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private HinhAnh getBy(string whereCom, string orderCom)
        {
            HinhAnh data = null;
            string comStr = "Select * from [HinhAnh]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    dap.DaReader.Read();
                    data = new HinhAnh();

                    data.IDHinhAnh = Convert.ToInt32(dap.DaReader["IDHinhAnh"]);
                    data.DuongDan = dap.DaReader["DuongDan"].ToString();
                    data.ChuThich = dap.DaReader["ChuThich"].ToString();
                    data.MaSanPham = dap.DaReader["MaSanPham"].ToString();

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public HinhAnh getByKey(int IDHinhAnh)
        {
            return getBy(" [IDHinhAnh]=" + IDHinhAnh.ToString(), " [IDHinhAnh] asc");
        }

        public HinhAnh getByKey(string MaSanPham)
        {
            return getBy(" [MaSanPham]='" + MaSanPham + "' ", " [IDHinhAnh] asc");
        }

    }
}